
import 'package:boss/main.dart';
import 'package:boss/provider/login_user_provider.dart';
import 'package:boss/ui/page/takeoutManagement/page/businessSetting/page/timeRange/model/hour_minute.dart';
import 'package:boss/ui/page/takeoutManagement/page/businessSetting/page/timeRange/time_range_page.dart';
import 'package:boss/ui/page/takeoutManagement/page/businessSetting/widget/business_settine_time.dart';
import 'package:boss/ui/widget/form/select_form_field.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:boss/ui/widget/form/title_switch.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class MachineSetupIndex extends StatefulWidget {
  @override
  _State createState() => _State();
}

class _State extends State<MachineSetupIndex> {
  int selectTextStatus = 0;

  dynamic data;

  //营业时间
  List<String> businessTime = ["00:00:00", "00:00:00"];

  //营业状态
  bool businessStatus = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(Duration(milliseconds: 0), () async {
      storeGetFun();
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  //获取等级优惠
  storeGetFun() async {
    try {
      var loginUserProvider = Provider.of<LoginUserProvider>(
        context,
        listen: false,
      );
      showLoadingToast();
      var res = await api.storeGet(loginUserProvider.loginUser.storeId);

      if (res.successful()) {
        data = res.data ?? null;
        selectTextStatus = res.data["dataTime"] ?? 0;
        businessTime[0] = res.data["openTime"] ?? '';
        businessTime[1] = res.data["closeTime"] ?? '';
        businessStatus = (res.data["storeStatus"] == 1 ? true : false) ?? false;
        setState(() {});
      } else {
        toast(res.msg);
      }
    } catch (e) {} finally {
      hideLoadingToast();
    }
  }

  //更新dataTime int
  dataTimeUpdate(index) async {
    showLoadingToast();
    data["dataTime"] = index;
    try {
      var res = await api.storeUpdate(data);
      if (res.successful()) {
        selectTextStatus = index;
        toast("修改成功");
        setState(() {});
      } else {
        storeGetFun();
        toast(res.msg);
      }
    } catch (e) {} finally {
      hideLoadingToast();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(
        left: 30.w,
        right: 30.w,
      ),
      child: Column(
        children: <Widget>[
          Container(
            child: SelectFormField<String>(
              title: '收银机数据时间设置',
              hintText: '请选择',
              items: [
                {"name": "不限制", "key": 0},
                {"name": "仅查看今天数据", "key": 1},
                {"name": "仅查看最近2天数据", "key": 2},
                {"name": "仅查看最近7天数据", "key": 3},
                {"name": "仅查看最近30天数据", "key": 4},
              ].map((item) => SelectItem(value: item["key"].toString(), title: item["name"])).toList(),
              value: selectTextStatus.toString(),
              onChanged: (value) {
                dataTimeUpdate(int.parse(value));
                setState(() {});
              },
            ),
          ),
          BusinessSettingTime(
            start: HourMinute(int.parse(businessTime[0].substring(0, 2) ?? 0), int.parse(businessTime[0].substring(3, 5) ?? 0)),
            end: HourMinute(int.parse(businessTime[1].substring(0, 2) ?? 0), int.parse(businessTime[1].substring(3, 5) ?? 0)),
            onTap: () async {
              if (data == null) {
                return;
              }
              var resData = await Navigator.pushNamed(context, TimeRangePage.pageName, arguments: {
                'start': HourMinute(int.parse(businessTime[0].substring(0, 2) ?? 0), int.parse(businessTime[0].substring(3, 5) ?? 0)),
                'end': HourMinute(int.parse(businessTime[1].substring(0, 2) ?? 0), int.parse(businessTime[1].substring(3, 5) ?? 0)),
              }) as List<HourMinute>;
              if (resData != null) {
                data["openTime"] = resData[0].toString() + ':00';
                data["closeTime"] = resData[1].toString() + ':59';
                showLoadingToast();
                try {
                  var res = await api.storeUpdate(data);
                  if (res.successful()) {
                    businessTime[0] = resData[0].toString() + ':00';
                    businessTime[1] = resData[1].toString() + ':59';
                    toast("修改成功");
                    setState(() {});
                  } else {
                    toast(res.msg);
                  }
                } catch (e) {} finally {
                  hideLoadingToast();
                }
              }
            },
          ),
          TitleSwitch(
            title: '营业状态', //外卖开启营业 外卖关闭营业
            value: businessStatus,
            onChanged: (value) async {
              data["storeStatus"] = (businessStatus == true ? 0 : 1);
              showLoadingToast();
              try {
                var res = await api.storeUpdate(data);
                if (res.successful()) {
                  businessStatus = !businessStatus;
                  toast("修改成功");
                  setState(() {});
                } else {
                  toast(res.msg);
                }
              } catch (e) {} finally {
                hideLoadingToast();
              }
            },
          ),
        ],
      ),
    );
  }
}
